<!--
 * @Descripttion: 房源轮播图
 * @Author: xiao li
 * @Date: 2020-07-06 12:17:07
 * @LastEditors: xiao li
 * @LastEditTime: 2021-04-29 13:50:34
-->
<template>
  <div>
    <top-nav></top-nav>
    <div class="page-main">
      <el-row class="page-top-operate">
        <lb-button
          type="primary"
          @click="addEditBanner('add')"
          icon="el-icon-plus"
          >新增轮播图</lb-button
        >
      </el-row>
      <el-table
        v-loading="loading"
        :data="tableData"
        :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
        style="width: 100%"
      >
        <el-table-column prop="id" label="ID"> </el-table-column>
        <el-table-column label="位置">
          <template slot-scope="scope">
            <span>{{
              scope.row.type === '1' ? '房源首页广告位' : '该位置未开发'
            }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="address" label="图片">
          <template slot-scope="scope">
            <lb-image :src="scope.row.path[0]" />
          </template>
        </el-table-column>
        <el-table-column label="跳转类型">
          <template slot-scope="scope">
            <span>{{ scope.row.link_type | handleLinkType }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="link" label="跳转链接"> </el-table-column>
        <el-table-column prop="top" label="排序值"> </el-table-column>
        <el-table-column prop="status" label="是否启用">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              active-value="1"
              inactive-value="0"
              @change="updateBnner(scope.row.status, scope.row.id)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <div class="table-operate">
              <lb-button
                type="primary"
                size="mini"
                plain
                @click="addEditBanner('edit', scope.row.id)"
                >{{ $t('action.edit') }}</lb-button
              >
              <lb-button
                type="danger"
                size="mini"
                plain
                @click="confirmDel(scope.row.id)"
                >{{ $t('action.delete') }}</lb-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <lb-page
        :batch="false"
        :page="page"
        :pageSize="limit"
        :total="total"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      >
      </lb-page>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      appPath: '',
      loading: false,
      tableData: [],
      page: 1,
      limit: 10,
      total: 0
    }
  },
  created () {
    let { path } = this.$route
    this.appPath = path.includes('app/') ? '/app' : ''
  },
  activated () {
    this.getTableDataList()
  },
  methods: {
    getTableDataList () {
      this.loading = true
      let { page, limit } = this
      this.$api.getHouseBannerList({ page, limit }).then(res => {
        console.log(res)
        this.loading = false
        if (res.code === 200) {
          this.tableData = res.data.data
          this.total = res.data.total
        }
      })
    },
    // 上下架、删除
    updateBnner (status, id) {
      this.$api.getEditHouseBanner({ status, id }).then(res => {
        console.log(res)
        if (res.code === 200) {
          this.$message.success(this.$t(status === -1 ? 'tips.successDel' : 'tips.successOper'))
          if (status === -1) {
            this.page = this.page < Math.ceil((this.total - 1) / this.limit) ? this.page : Math.ceil((this.total - 1) / this.limit)
            this.getTableDataList()
          }
        } else {
          // if (status === -1) return
          this.getTableDataList()
        }
      })
    },
    addEditBanner (type, id) {
      let { appPath } = this
      let url = type === 'add' ? `${appPath}/house/editBanner` : `${appPath}/house/editBanner?id=${id}`
      this.$router.push(url)
    },
    // 删除
    confirmDel (id) {
      this.$confirm(this.$t('tips.confirmDelete'), this.$t('tips.reminder'), {
        confirmButtonText: this.$t('action.comfirm'),
        cancelButtonText: this.$t('action.cancel'),
        type: 'warning'
      }).then(() => {
        this.updateBnner(-1, id)
      })
    },
    handleSizeChange (val) {
      this.limit = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.page = val
      this.getTableDataList()
    }
  },
  filters: {
    handleLinkType: function (type) {
      let result = ''
      switch (type) {
        case '1':
          result = '拨打电话'
          break
        case '2':
          result = '跳转到其他小程序'
          break
        case '3':
          result = '跳转网页'
          break
        case '4':
          result = '跳转系统内部页面'
          break
      }
      return result
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
